package com.caoheng.pay.pay.channel;

import com.alibaba.fastjson.JSONObject;
import com.caoheng.pay.pay.channel.wrap.request.AliPayRequestParam;
import org.springframework.util.Assert;

import static org.springframework.util.StringUtils.isEmpty;

/**
 * @ClassName AliPayChannel
 * @Description alipay支付渠道处理
 * @Author shuimodanqing
 * @Date 2018/12/20 1:16 PM
 */
public class AliPayChannel implements PayChannel {




    @Override
    public <E> E executeForPay(JSONObject jsonObject) {
        AliPayRequestParam requestParam = resolveParamForAliPay(jsonObject);
        return null;
    }

    @Override
    public void executeForRefund() {

    }

    @Override
    public void executeForRefundQuery() {

    }

    @Override
    public void executeForPayOrderQuery() {

    }

    private AliPayRequestParam resolveParamForAliPay(JSONObject requestParam){
        //一.请求参数的封装

        //1.应用appId(支付宝分配给开发者的应用ID)
        String appId = requestParam.getString("appId");
        Assert.notNull(appId,"appId si not null!");

        //2.支付宝网关(请求支付宝进行处理的接口)
        String gateWay = requestParam.getString("gateway");
        Assert.notNull(gateWay,"gateWay si not null!");

        //3.个人私钥(加签使用)
        String privateKey = requestParam.getString("privateKey");
        Assert.notNull(privateKey,"privateKey si not null!");

        //4.仅支持JSON
        String format = requestParam.getString("format");
        format = isEmpty(format) ? "JSON" : format;
        //5.编码集
        String charset = requestParam.getString("charset");
        charset = isEmpty(charset) ? "utf-8" : charset;

        //6.支付宝公钥
        String aliPublicKey = requestParam.getString("publicKey");
        Assert.notNull(aliPublicKey,"支付宝公钥不能为空 for param aliPublicKey");

        //7.签名类型
        String signType = requestParam.getString("signType");
        Assert.notNull(signType,"签名类型不能为空: signType");

        //二.请求model的封装(具体的商品信息的描述)
        String body = requestParam.getString("body");
        Assert.notNull(body,"body is not null!");

        String notifyUrl = requestParam.getString("notifyUrl");

        String subject = body;

        String outTradeNo = requestParam.getString("outTradeNo");
        Assert.notNull(outTradeNo,"outTradeNo is not null!");

        String timeOutExpress =  requestParam.getString("timeOutExpress");

        String timeExpire = requestParam.getString("timeExpire");

        String totalAmount = requestParam.getString("totalFee");
        Assert.notNull(totalAmount,"支付金额不能为空");

        String productCode = "QUICK_MSECURITY_PAY";

        String goodsType = "1";

        return new AliPayRequestParam.Builder()
                .appId(appId).aliPublicKey(aliPublicKey).body(body).format(format).goodsType(goodsType).notifyUrl(notifyUrl)
                .outTradeNo(outTradeNo).privateKey(privateKey).productCode(productCode).signType(signType).subject(subject)
                .timeExpire(timeExpire).timeoutExpress(timeOutExpress).gateWay(gateWay)
                .build();

    }
}
